//
//  ViewController.m
//  TableSelectDemo
//
//  Created by ebeijia on 15/11/26.
//  Copyright © 2015年 ebeijia. All rights reserved.
//

#import "ViewController.h"
#import "MyTableViewCell.h"
#import "NotifyModel.h"
#import "ViewController1.h"
#import "aaaaaaaaaaaaaaaViewController.h"
#import <SystemConfiguration/CaptiveNetwork.h>

#import <CoreTelephony/CTCarrier.h>
#import <CoreTelephony/CTTelephonyNetworkInfo.h>

#include <sys/socket.h> // Per msqr
#include <sys/sysctl.h>
#include <net/if.h>
#include <net/if_dl.h>

#pragma mark MAC addy

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate,viewDelegate>
{
    UITableView *_tableView;
    NSMutableArray *_notifyModels;
    UIWebView *webView;
}
@end

@implementation ViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    
}

- (void)viewDelegate
{
    [_tableView reloadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
   
    
    UIButton *b = [UIButton buttonWithType:UIButtonTypeContactAdd];
    b.frame = CGRectMake(100, 250, 50, 50);
    [b addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:b];
    
    
    
    
    

    
    
    
    
    
    
    
    
    
    _notifyModels = [NSMutableArray array];
    
    for (int i = 0; i < 6; i ++) {
        NotifyModel *model = [[NotifyModel alloc] init];
        model.isRead = @"0";
        model.contentId = [NSString stringWithFormat:@"%d",(i + 1) * 10];
        [_notifyModels addObject:model];
    }

    _tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.rowHeight = 70;
    //[self.view addSubview:_tableView];
    
}

-(void)buttonClick
{
    NSLog(@"mmmmmmmmmmaaaaaaaaaaaaaannnnnnnnnnnnnnnn");
    
    aaaaaaaaaaaaaaaViewController *vc = [[aaaaaaaaaaaaaaaViewController alloc] init];
    
    UIView *view = [[UIApplication sharedApplication] keyWindow];
    //view.userInteractionEnabled = YES;
    //vc.view.backgroundColor = [UIColor clearColor];
    vc.view.backgroundColor = [UIColor blackColor];
    vc.view.alpha = 0.5;
    [view addSubview:vc.view];
}



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 6;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myTableView"];
    if (!cell) {
        cell = [[MyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"myTableView"];
    }
    
    NotifyModel *notifyModel = _notifyModels[indexPath.row];
    
//    NSArray *contentIdArr  = [self getLocalData:@"kContentIdArr"];
//    NSLog(@"contentIdArr =============== %@",contentIdArr);
//    for (int i = 0; i<contentIdArr.count; i++) {
//
//        NSString *docuContentId = contentIdArr[i];
//        for (int j = 0; j < _notifyModels.count; j++) {
//
//            NSString *contentId = notifyModel.contentId;
//            NSLog(@"%@,,,,,,,%@",docuContentId,contentId);
//
//            if ([docuContentId isEqualToString:contentId]) {
//                notifyModel = _notifyModels[j];
//                notifyModel.isRead = @"1";
//
//            }else {
////                notifyModel.isRead = @"0";
//            }
//            
//        }
//    }
    
    [cell drawCell:notifyModel];
    
    
    return cell;
}

- (nullable NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @[@"11111",@"22222"];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //
    NotifyModel *notifyModel = _notifyModels[indexPath.row];
    //数据存储
    //1
    notifyModel.isRead = @"1";
    //2
    NSArray *contentIdArr  = [self getLocalData:@"kContentIdArr"];
    NSMutableArray *tempArr = [NSMutableArray arrayWithArray:contentIdArr];
    
    
    if (![tempArr containsObject:notifyModel.contentId]) {
        [tempArr addObject:notifyModel.contentId];
        [self saveLocalData:@"kContentIdArr" value:tempArr];
    }
    
    ViewController1 *vc= [[ViewController1 alloc] init];
    vc.delegate = self;
    [self.navigationController pushViewController:vc animated:YES];
}



- (void)saveLocalData:(NSString *)key value:(id)value {
    [[NSUserDefaults standardUserDefaults] setObject:value forKey:key];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

- (id)getLocalData:(NSString * )key {
    return [[NSUserDefaults standardUserDefaults] objectForKey:key];
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
